﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;
using VIW = CPPEI.ETL.Domain.Views;

namespace CPPEI.ETL.WebApi
{
    [CPPEI.WebApi.AuthGetAction("ETL", "GetDataTableList", "1.0")]
    public class GetDataTableListAction : BaseAction<GetDataTableListParams, GetDataTableListResult>
    {
        protected override ActionResult<GetDataTableListResult> OnExecute(ActionContext context, 
            SysParameters sysParameters,
            GetDataTableListParams opParams)
        {
            SER.DtTableService service = new SER.DtTableService();
            PagedList<VIW.VDtTable2> result = service.GetPagedVTables(opParams.Filter, opParams.Page * 25, 25);
            if(result.IsSuccess)
            {
                GetDataTableListResult actResult = new GetDataTableListResult();
                actResult.Start = result.Start;
                actResult.Limit = result.Limit;
                actResult.Total = result.Total;
                actResult.Page = opParams.Page;

                foreach (var eItem in result.Data)
                {
                    GetDataTableListResult.Item item = new GetDataTableListResult.Item();
                    item.Id = eItem.DataTable.Id;
                    item.Name = eItem.DataTable.Name;
                    item.Creator = eItem.DataTable.Creator;
                    item.CreatedTime = eItem.DataTable.CreatedTime;
                    item.IsRawDt = eItem.DataTable.IsRawDt;

                    item.DataSources = new List<GetDataTableListResult.DataSource>();
                    for (int index = 0; index < eItem.DataSources.Count; index++)
                    {
                        GetDataTableListResult.DataSource dataSource = new GetDataTableListResult.DataSource();
                        if (!string.IsNullOrEmpty(eItem.DataSources[index].Name))
                        {
                            dataSource.DataSourceName = eItem.DataSources[index].Name;
                        }
                        else
                        {
                            dataSource.DataSourceName = "";
                        }

                        dataSource.DataSourceId = eItem.DataSources[index].Id;

                        dataSource.DataServiceId = eItem.DataSources[index].DtSerId;
                        item.DataSources.Add(dataSource);
                    }

                    actResult.Items.Add(item);
                }

                return new ActionResult<GetDataTableListResult>(actResult);
            }
            else
            {
                return new ActionResult<GetDataTableListResult>(result.IsSuccess, result.ErrorCode, result.Message, null);
            }
        }
    }

    public class GetDataTableListParams
    {
        [Parameter]
        public int Page { get; set; }

        [Parameter]
        public string Filter { get; set; }
    }

    public class GetDataTableListResult : PagedResult
    {
        public List<Item> Items = new List<Item>();

        public class Item
        {
            public Guid Id { get; set; }
            public string Name { get; set; }
            public string Creator { get; set; }

            public DateTime CreatedTime { get; set; }

            public bool IsRawDt { get; set; }

            public List<DataSource> DataSources { get; set; }
        }

        public class DataSource
        {
            public Guid DataSourceId { get; set; }
            public string DataSourceName { get; set; }
            public Guid DataServiceId { get; set; }
        }
    }
}
